package za.ac.wits.elen7045.xml.writer;

/**
 * @author ed
 * @Date 12/6/2012
 * 
 * This is an STAX XML implementation class lass for the Error Handling Framework.
 * The framework is based on the Template Method design Pattern. This class will create 
 * xml files of all runtime exceptions aka errors in za.ac.wits.elen7045.exceptions.
 *
 */

import java.io.FileOutputStream;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import za.ac.wits.elen7045.exceptions.APSException;

public class APSErrorToXml {
	
	private String apsXmlErrorFileName;
	private String apsErrorCode, apsErrorDate, apsBaseUrl, apsErrorTime;
	
	
	public void createXmlErrorFile(APSException anApsError) {
		
		apsErrorCode = anApsError.getErrorCode();
		apsErrorDate = anApsError.getDate();
		apsErrorTime = anApsError.getTime();
		apsBaseUrl = anApsError.getBaseUrl();
		apsXmlErrorFileName = anApsError.getErrorCode() + apsErrorTime + ".xml";
		

	}
	
	public void generateXmlErrorConfig() throws Exception {
		
		XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
		
		XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(apsXmlErrorFileName));
		// Create a EventFactory
		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createCharacters("\n");
		// Create and write Start Tag
		StartDocument startDocument = eventFactory.createStartDocument();
		eventWriter.add(startDocument);

		//Create aps-error xml tag
		StartElement apsErrorStartElement = eventFactory.createStartElement("", "", "aps-error");
		eventWriter.add(end);
		eventWriter.add(apsErrorStartElement);
		eventWriter.add(end);
		
		//Write the different nodes
		createXmlNode(eventWriter, "base-url", apsBaseUrl);
		createXmlNode(eventWriter, "date", apsErrorDate);
		createXmlNode(eventWriter, "time", apsErrorTime);
		createXmlNode(eventWriter, "error-code", apsErrorCode);

		eventWriter.add(eventFactory.createEndElement("", "", "aps-error"));
		eventWriter.add(end);
		eventWriter.add(eventFactory.createEndDocument());
		eventWriter.close();
	}
	
	private void createXmlNode(XMLEventWriter anXmlEventWriter, String anXmlName,
			String value) throws XMLStreamException {

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createCharacters("\n");
		XMLEvent tab = eventFactory.createCharacters("\t");
		// Create Start node
		StartElement startElement = eventFactory.createStartElement("", "", anXmlName);
		anXmlEventWriter.add(tab);
		anXmlEventWriter.add(startElement);
		//Generate Xml Error Content
		Characters characters = eventFactory.createCharacters(value);
		anXmlEventWriter.add(characters);
		//Generate Xml End element
		EndElement endElement = eventFactory.createEndElement("", "", anXmlName);
		anXmlEventWriter.add(endElement);
		anXmlEventWriter.add(end);

	}

}
